package com.yuyou.fn.portal.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mq.MessageHandler;
import com.github.wens.mq.RedisMessageQueue;

import com.yuyou.fn.common.util.*;

import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;

import com.yuyou.fn.portal.repository.OuterTeacherRepository;

import com.yuyou.fn.portal.service.IOuterTeacherService;
import com.yuyou.fn.portal.vo.*;
import org.apache.commons.lang3.StringUtils;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/**
 * Created by wens on 2017/4/6.
 */
@Service
public class OuterTeacherServiceImpl implements IOuterTeacherService {

    @Resource
    private RedisMessageQueue redisMessageQueue ;

    private  HashMap<Integer,String> timeMap=new HashMap<Integer, String>();

    @Resource
    private MongoTemplate mongoTemplate;



    @Resource
    private IAreaTeamService areaTeamService;


    @Resource
    private OuterTeacherRepository outerTeacherRepository;

    @PostConstruct
    public  void init(){
        redisMessageQueue.consume("teacher","teacherChannel3",new MessageHandler() {
            @Override
            public void onMessage(byte[] bytes) {
                JSONObject jsonObject = JSONS.toJSONObject(bytes);
                String event = jsonObject.getString("event");

                if("insert".equals(event) || "update".equals(event)){
                    JSONArray rowList = jsonObject.getJSONArray("rowList");
                    List<OuterTeacher> outerTeacherList = toTeacher(rowList, false );
                    outerTeacherRepository.save(outerTeacherList);

                }else if("delete".equals(event)){
                    JSONArray rowList = jsonObject.getJSONArray("rowList");
                    List<OuterTeacher> outerTeacherList = toTeacher(rowList, true );
                    outerTeacherRepository.delete(outerTeacherList);
                }
            }
            private List<OuterTeacher> toTeacher(JSONArray rowList , boolean isDelete) {
                List<OuterTeacher> outerTeacherList = new ArrayList<>(rowList.size());
                for (int i = 0; i < rowList.size(); i++) {
                    JSONObject row = processFiled(rowList.getJSONObject(i) );
                    OuterTeacher outerTeacher = row.toJavaObject(OuterTeacher.class);
                    if(!isDelete){
                        fillExtra(outerTeacher);
                    }
                    outerTeacherList.add(outerTeacher);
                }
                return outerTeacherList;
            }
            private JSONObject processFiled(JSONObject row) {
                JSONObject nRow = new JSONObject();
                for(String key : row.keySet()){
                    nRow.put(Utils.camelCase(key),row.get(key));
                }
                return nRow ;
            }
        });
    }
    private void fillExtra(OuterTeacher outerTeacher) {
        //填充城市
        if (outerTeacher.getAreaTeamId()!=null){
            AreaTeam areaTeam=areaTeamService.findById(outerTeacher.getAreaTeamId());
            outerTeacher.setCity(areaTeam.getCity());
        }
    }

    @Override
    public List<OuterTeacher> findTeacherByCond(int pageNo, int pageSize, OuterTeacher outerTeacher) {
        Criteria criteria = new Criteria();
        Query query=new Query();
        if(StringUtils.isNotEmpty(outerTeacher.getCity())){
            query.addCriteria(criteria.where("city").is(outerTeacher.getCity()));
        }
        if(StringUtils.isNotEmpty(outerTeacher.getTeacherName())){
            query.addCriteria(criteria.where("teacherName").regex(".*?" +outerTeacher.getTeacherName()+ ".*"));
        }
        if(outerTeacher.getStatus()!=null){
            query.addCriteria(criteria.where("status").is(outerTeacher.getStatus()));
        }
        return   mongoTemplate.find(query.skip(pageNo*pageSize).limit(pageSize),OuterTeacher.class);
    }
}
